package com.lms.vo;

import com.lms.bean.Book;
import com.lms.bean.ReturnRecord;
import com.lms.bean.User;
import org.springframework.beans.BeanUtils;

/**
 * 视图层对象(View Object)
 * 用于传输到前端页面
 * 为了避免转化成JSON时陷入无穷递归链，所有的的视图层对象(VO)中都不能包含对象字段(例如Book,User)
 */

public class ReturnRecordVO extends ReturnRecord {
    private Long userId;
    private String username;
    private String bookName;
    private Long bookId;
    private Long bookSumNumber;

    /**
     * 将 Bean 转化成 VO
     */

    public static ReturnRecordVO convert(ReturnRecord returnRecord) {
        ReturnRecordVO returnRecordVO = new ReturnRecordVO();
        BeanUtils.copyProperties(returnRecord, returnRecordVO);

        returnRecordVO.setBook(null);
        Book book = returnRecord.getBook();
        if (book != null) {
            returnRecordVO.setBookId(book.getBookId());
            returnRecordVO.setBookSumNumber(book.getBookSumNumber());
            returnRecordVO.setBookName(book.getBookName());
        }

        returnRecordVO.setUser(null);
        User user = returnRecord.getUser();
        if (user != null) {
            returnRecordVO.setUserId(user.getUserId());
            returnRecordVO.setUsername(user.getUsername());
        }
        return returnRecordVO;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public Long getBookId() {
        return bookId;
    }

    public void setBookId(Long bookId) {
        this.bookId = bookId;
    }

    public Long getBookSumNumber() {
        return bookSumNumber;
    }

    public void setBookSumNumber(Long bookSumNumber) {
        this.bookSumNumber = bookSumNumber;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
}
